package com.lahuan.service.impl;

import com.lahuan.config.datasouce.CustomTransaction;
import com.lahuan.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author lfy
 */
@Component
public class TestServiceImpl implements TestService {

    @Resource(name="jdbcTemplate1")
    JdbcTemplate jdbcTemplate1;
    @Resource(name="jdbcTemplate2")
    JdbcTemplate jdbcTemplate2;
    /**
     * 注入自己，通过spring调用才能使aop生效
     */
    @Autowired
    TestServiceImpl testService;


    @Override
    public void initData() {
        jdbcTemplate1.execute("truncate table test_transaction;");
        jdbcTemplate2.execute("truncate table test_transaction;");

        jdbcTemplate1.execute("INSERT INTO `test_transaction`(`id`, `content`, `content_b`) VALUES (1, 'init_a_1', 'init_a_1');");
        jdbcTemplate1.execute("INSERT INTO `test_transaction`(`id`, `content`, `content_b`) VALUES (2, 'init_a_2', 'init_a_2');");

        jdbcTemplate2.execute("INSERT INTO `test_transaction`(`id`, `content`, `content_b`) VALUES (1, 'init_b_1', 'init_b_1');");
        jdbcTemplate2.execute("INSERT INTO `test_transaction`(`id`, `content`, `content_b`) VALUES (2, 'init_b_2', 'init_b_2');");

    }

    @CustomTransaction
    @Override
    public void testSuccess(int a,int b) {
        System.out.println("进入testSuccess(),参数:"+a+","+b);
        jdbcTemplate1.execute("delete from `test_transaction` WHERE `id` = 100;");
        jdbcTemplate2.execute("UPDATE `test_transaction` SET `content` = 'not exist' WHERE `id` = 100;");
        testService.runSql1();
        testService.runSql2();
    }

    private void triggerException() {
        throw  new RuntimeException("模拟出现异常");
    }

    @CustomTransaction
    @Override
    public void testError(int a, int b) {
        System.out.println("进入testError(),参数:"+a+","+b);
        jdbcTemplate1.execute("delete from `test_transaction` WHERE `id` = 100;");
        jdbcTemplate2.execute("UPDATE `test_transaction` SET `content` = 'not exist' WHERE `id` = 100;");
        testService.runSql1();
        testService.runSql2();
        triggerException();
    }

    @CustomTransaction
    @Override
    public void runSql1() {
        jdbcTemplate1.execute("INSERT INTO `test_transaction` (  `content`, `content_b` ) VALUES (  'runSql1', 'insert');");
        jdbcTemplate1.execute("UPDATE `test_transaction` SET `content` = 'runSql1', `content_b` = 'runSql1 update' WHERE `id` = 1;");
        jdbcTemplate1.execute("delete from `test_transaction` WHERE `id` = 2;");
    }

    @CustomTransaction
    @Override
    public void runSql2() {
        jdbcTemplate2.execute("INSERT INTO `test_transaction` (  `content`, `content_b` ) VALUES (  'runSql2', 'insert');");
        jdbcTemplate2.execute("UPDATE `test_transaction` SET `content` = 'runSql2', `content_b` = 'runSql2 update' WHERE `id` = 1;");
        jdbcTemplate2.execute("delete from `test_transaction` WHERE `id` = 2;");
    }

}
